import { defineStore } from 'pinia'
import * as menuApi from '../../../api/system/menu'
import type { Menu } from '../../../types/app.d'

// 菜单管理 Store 接口
interface MenuManagementState {
  menuList: Menu[]
  menuTree: Menu[]
  userMenus: Menu[]
  loading: boolean
  currentMenu: Menu | null
  menuSelectTree: Menu[]
}

export const useMenuStore = defineStore('menuManagement', {
  state: (): MenuManagementState => ({
    menuList: [],
    menuTree: [],
    userMenus: [],
    loading: false,
    currentMenu: null,
    menuSelectTree: []
  }),

  getters: {
    // 获取菜单列表
    getMenuList(): Menu[] {
      return this.menuList
    },

    // 获取菜单树
    getMenuTree(): Menu[] {
      return this.menuTree
    },

    // 获取用户菜单
    getUserMenus(): Menu[] {
      return this.userMenus
    },

    // 获取加载状态
    getLoading(): boolean {
      return this.loading
    },

    // 获取当前菜单
    getCurrentMenu(): Menu | null {
      return this.currentMenu
    },

    // 获取菜单选择树
    getMenuSelectTree(): Menu[] {
      return this.menuSelectTree
    }
  },

  actions: {
    // 获取菜单列表
    async getMenuListAction(params?: {
      status?: number
      name?: string
      type?: number
    }): Promise<void> {
      this.loading = true
      try {
        const response = await menuApi.getMenuList(params)
        if (response.code === 200) {
          this.menuList = response.data || []
        }
      } catch (error) {
        console.error('获取菜单列表失败:', error)
      } finally {
        this.loading = false
      }
    },

    // 获取菜单树
    async getMenuTreeAction(params?: {
      status?: number
      type?: number
    }): Promise<void> {
      try {
        const response = await menuApi.getMenuTree(params)
        if (response.code === 200) {
          this.menuTree = response.data || []
        }
      } catch (error) {
        console.error('获取菜单树失败:', error)
      }
    },

    // 获取用户菜单
    async getUserMenusAction(): Promise<void> {
      try {
        const response = await menuApi.getUserMenus()
        if (response.code === 200) {
          this.userMenus = response.data || []
        }
      } catch (error) {
        console.error('获取用户菜单失败:', error)
      }
    },

    // 获取菜单详情
    async getMenuInfoAction(id: string): Promise<void> {
      try {
        const response = await menuApi.getMenuInfo(id)
        if (response.code === 200) {
          this.currentMenu = response.data
        }
      } catch (error) {
        console.error('获取菜单详情失败:', error)
      }
    },

    // 创建菜单
    async createMenuAction(data: Omit<Menu, 'id' | 'createTime' | 'updateTime' | 'children'>): Promise<boolean> {
      try {
        const response = await menuApi.createMenu(data)
        if (response.code === 200) {
          return true
        }
        return false
      } catch (error) {
        console.error('创建菜单失败:', error)
        return false
      }
    },

    // 更新菜单
    async updateMenuAction(id: string, data: Partial<Omit<Menu, 'id' | 'createTime' | 'children'>>): Promise<boolean> {
      try {
        const response = await menuApi.updateMenu(id, data)
        if (response.code === 200) {
          return true
        }
        return false
      } catch (error) {
        console.error('更新菜单失败:', error)
        return false
      }
    },

    // 删除菜单
    async deleteMenuAction(id: string): Promise<boolean> {
      try {
        const response = await menuApi.deleteMenu(id)
        if (response.code === 200) {
          return true
        }
        return false
      } catch (error) {
        console.error('删除菜单失败:', error)
        return false
      }
    },

    // 获取菜单选择树
    async getMenuSelectTreeAction(params?: {
      excludeId?: string
    }): Promise<void> {
      try {
        const response = await menuApi.getMenuSelectTree(params)
        if (response.code === 200) {
          this.menuSelectTree = response.data || []
        }
      } catch (error) {
        console.error('获取菜单选择树失败:', error)
      }
    },

    // 清空当前菜单
    clearCurrentMenu(): void {
      this.currentMenu = null
    }
  }
})